package com.example.goods_service.controller;

import com.example.goods_service.return_data.Mall_Data;
import com.example.goods_service.service.GoodsService_oyhl;
import com.example.goods_service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @ClassName GoodsController_oyhl
 * @Author Ninja
 * @date 2021.12.07 08:05
 */
@RestController
@Api
public class GoodsController_oyhl {
    @Autowired
    GoodsService_oyhl goodsService_oyhl;

    @ApiOperation("首页搜索框查询提示分类")
    @RequestMapping("/shopss")
    public Mall_Data mall_data(@RequestParam(value = "category_name", required = false) String category_name, TCommodity tCommodity) {
        return new Mall_Data(200, "asda",goodsService_oyhl.shop(category_name));
    }

    @ApiOperation("搜索记录查询 根据用户id")
    @RequestMapping("/ls")
    public Mall_Data ls(@RequestParam(value = "userid", required = false) int userid) {
        return new Mall_Data(200, "sada",goodsService_oyhl.ls(userid));
    }

    @ApiOperation("搜索记录添加 重复不添加")
    @RequestMapping("/indata")
    public Mall_Data indata(@RequestParam(value = "userid", required = false) Integer userid,
                            @RequestParam(value = "value", required = false) String value,
                            @RequestParam(value = "time", required = false) String time) {
        if (goodsService_oyhl.sel(userid, value) != null && goodsService_oyhl.sel(userid, value) >= 1) {
            return new Mall_Data(500, "错误",null);
        }
        return new Mall_Data(200,"错误", goodsService_oyhl.indata(value, userid, time));
    }

    @ApiOperation("搜索记录删除")
    @RequestMapping("/dele")
    public Mall_Data dele(@RequestParam(value = "id", required = false) int id) {
        return new Mall_Data(200, "错误",goodsService_oyhl.dele(id));
    }

    @ApiOperation("分类搜索商品没有此分类进行模糊查询")
    @RequestMapping("/ppp")
    public Mall_Data ppp(@RequestParam(value = "category_name", required = false) String category_name) {
        if (goodsService_oyhl.sel1(category_name).size()>0) {
            List<TCommodity>ppp=goodsService_oyhl.ppp(category_name);
            return new Mall_Data(200,"错误", ppp);
        } else {
            return new Mall_Data(200, "错误",goodsService_oyhl.ppp1(category_name));
        }
    }

    @ApiOperation("搜索记录删除")
    @RequestMapping("/pppp")
    public Mall_Data pppp(@RequestParam(value = "category_name", required = false) String category_name) {
        if (goodsService_oyhl.sel1(category_name).size()>0) {
            return new Mall_Data(200,"错误", goodsService_oyhl.pppp(category_name));
        } else {
            return new Mall_Data(200, "错误",goodsService_oyhl.pppp1(category_name));
        }
    }

    @ApiOperation("遍历配置 筛选key value ")
    @RequestMapping("/www")
    public Mall_Data lsal(@RequestParam(value = "category_name", required = false) String category_name) {
        List<TAttributeDetails>list=goodsService_oyhl.list();
        List<TCategoryAttribute> list1=goodsService_oyhl.lsal(category_name);
        bulidmenu(list1,list);

        return new Mall_Data(200,"错误",list1);
    }

    private  List<TCategoryAttribute> bulidmenu(List<TCategoryAttribute> list1, List<TAttributeDetails> list)
    {
        for (TCategoryAttribute tCategoryAttribute : list1)
        {
            for (TAttributeDetails tAttributeDetails : list)
            {
                if (tAttributeDetails.getAttribute_id()==tCategoryAttribute.getId()){
                    tCategoryAttribute.getList().add(tAttributeDetails);
                }
            }

        }
        return list1;
    }

    @ApiOperation("分类查询")
    @RequestMapping("/fl")
    public Mall_Data fl(@RequestParam(value = "category_name", required = false) String category_name) {
        List<T_commodity_category>list=goodsService_oyhl.fl();
        List<T_commodity_category>list1 = new ArrayList<>();
        for (T_commodity_category category : list)
        {
            if (category.getCategory_parent_ID()==0)
            {
                category = bulidmenusss(category,list);
                list1.add(category);
            }
        }

        return new Mall_Data(200,"错误",list1);
    }

    private T_commodity_category bulidmenusss(T_commodity_category category, List<T_commodity_category> list)
    {
        for (T_commodity_category t_commodity_category : list) {

            if (category.getId()==t_commodity_category.getCategory_parent_ID()){
                t_commodity_category = bulidmenusss(t_commodity_category,list);
                category.add(t_commodity_category);
            }
        }
        return category;
    }


    @ApiOperation("遍历属性 筛选 key value ")
    @RequestMapping("/oooo")
    public Mall_Data oooo(@RequestParam(value = "category_name", required = false) String category_name) {
        List<T_category_config>oooo=goodsService_oyhl.oooo(category_name);
        List<T_config_details> oooo1=goodsService_oyhl.oooo1();
        bulidmenuss(oooo,oooo1);

        return new Mall_Data(200,"错误",oooo);
    }

    private List<T_category_config> bulidmenuss(List<T_category_config> oooo, List<T_config_details> oooo1)
    {
        for (T_category_config t_category_config : oooo)
        {
            for (T_config_details t_config_details : oooo1)
            {
                if (t_config_details.getConfig_id()==t_category_config.getId()){
                    t_category_config.getChild().add(t_config_details);
                }
            }
        }
        return oooo;
    }

    @ApiOperation("通过筛选条件查询 属性和配置")
    @RequestMapping("/sp")
    public Mall_Data sp(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                        @RequestParam(value = "config_details", required = false) String config_details,
                        @RequestParam(value = "attribute", required = false) String attribute)
    {
        return new Mall_Data(200,"错误",goodsService_oyhl.sp(commodity_name, config_details, attribute));
    }

    @ApiOperation("通过筛选条件查询 属性和配置 得到商品数量")
    @RequestMapping("/sp1")
    public Mall_Data sp1(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                        @RequestParam(value = "config_details", required = false) String config_details,
                        @RequestParam(value = "attribute", required = false) String attribute)
    {
        return new Mall_Data(200,"错误",goodsService_oyhl.sp1(commodity_name, config_details, attribute));
    }

    @ApiOperation("销量排序")
    @RequestMapping("/px")
    public Mall_Data px(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                         @RequestParam(value = "config_details", required = false) String config_details,
                         @RequestParam(value = "attribute", required = false) String attribute)
    {
        if (goodsService_oyhl.sel1(commodity_name).size()>0) {
            return new Mall_Data(200,"错误",goodsService_oyhl.px(commodity_name, config_details, attribute));
        } else {
            return new Mall_Data(200, "错误",goodsService_oyhl.px1(commodity_name, config_details, attribute));
        }
    }

    @ApiOperation("价格低到高")
    @RequestMapping("/di")
    public Mall_Data gao(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                        @RequestParam(value = "config_details", required = false) String config_details,
                        @RequestParam(value = "attribute", required = false) String attribute)
    {
        if (goodsService_oyhl.sel1(commodity_name).size()>0) {
            return new Mall_Data(200,"错误",goodsService_oyhl.gao(commodity_name, config_details, attribute));
        }
        else {
            return new Mall_Data(200, "错误",goodsService_oyhl.gao1(commodity_name, config_details, attribute));
        }
    }

    @ApiOperation("价格高到低")
    @RequestMapping("/gao")
    public Mall_Data di(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                         @RequestParam(value = "config_details", required = false) String config_details,
                         @RequestParam(value = "attribute", required = false) String attribute)
    {
        if (goodsService_oyhl.sel1(commodity_name).size()>0) {
            return new Mall_Data(200,"错误",goodsService_oyhl.di(commodity_name, config_details, attribute));        }
        else {
            return new Mall_Data(200, "错误",goodsService_oyhl.di1(commodity_name, config_details, attribute));
        }
    }

    @ApiOperation("价格大小自填")
    @RequestMapping("/jiage")
    public Mall_Data jiage(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                        @RequestParam(value = "config_details", required = false) String config_details,
                        @RequestParam(value = "attribute", required = false) String attribute,
                           @RequestParam(value = "xiao", required = false) Integer xiao,
                           @RequestParam(value = "da", required = false) Integer da)
    {
        if (xiao==null && da!=null){
            xiao=0;
        } else if (da==null && xiao!=null){
            da=999999999;
        }
        if (xiao==null && da==null){
            xiao=0;
            da=999999999;
        }
        if (goodsService_oyhl.sel1(commodity_name).size()>0) {
            return new Mall_Data(200,"错误",goodsService_oyhl.jiage(commodity_name, config_details, attribute,xiao,da));
        }
        else{
                return new Mall_Data(200, "错误", goodsService_oyhl.jiages(commodity_name, config_details, attribute,xiao,da));
            }
    }

    @ApiOperation("价格大小自填 得到的数量")
    @RequestMapping("/jiage1")
    public Mall_Data jiage1(@RequestParam(value = "commodity_name", required = false) String commodity_name,
                           @RequestParam(value = "config_details", required = false) String config_details,
                           @RequestParam(value = "attribute", required = false) String attribute,
                           @RequestParam(value = "xiao", required = false) Integer xiao,
                           @RequestParam(value = "da", required = false) Integer da)
    {
        if (xiao==null && da!=null){
            xiao=0;
        } else if (da==null && xiao!=null){
            da=999999999;
        }
        if (xiao==null && da==null){
            xiao=0;
            da=999999999;
        }
            return new Mall_Data(200,"错误",goodsService_oyhl.jiage1s(commodity_name, config_details, attribute,xiao,da));
    }
    /* ----------------------------后台查询---------------------------*/
    @ApiOperation("查询后台数据")
    @RequestMapping("/sssss")
    public Mall_Data sssss(@RequestParam(value = "creation_time", required = false) String creation_time,
                           @RequestParam(value = "commodity_name", required = false) String commodity_name,
                           @RequestParam(value = "userid",required = false)int userid)
    {
        List<TCommodity>list=goodsService_oyhl.ssssss(creation_time, commodity_name,userid);
        List<T_commodity_allocation> list1=goodsService_oyhl.sssss();
        System.out.println(list1);
        bulidmenus(list,list1);
        return new Mall_Data(200,"错误",list);
    }

    private List<TCommodity> bulidmenus(List<TCommodity> list, List<T_commodity_allocation> list1)
    {
        for (TCommodity commodity : list) {
            for (T_commodity_allocation t_commodity_allocation : list1) {
                if (commodity.getId()==t_commodity_allocation.getCommodity_name()){
                    commodity.getAsslist().add(t_commodity_allocation);
                }
            }
        }
        return list;
    }

    @ApiOperation("查询后台数量")
    @RequestMapping("/ssssss1")
    public Mall_Data ssssss1(@RequestParam(value = "creation_time", required = false) String creation_time,
                           @RequestParam(value = "commodity_name", required = false) String commodity_name,
                             @RequestParam(value = "userid",required = false)int userid)
    {
        return new Mall_Data(200,"错误",goodsService_oyhl.ssssss1(creation_time, commodity_name,userid));
    }
}



